package com.yejing.redisdemoTest;

import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.ListPosition;

import java.util.List;

@Slf4j
public class ListDemo {


    public Jedis jedis = null;

    @Before
    public void init() {
        if(null == jedis){
            DefaultJedisClientConfig config =
                    DefaultJedisClientConfig.builder() //
                            .password("123456") //
                            .build(); //
            HostAndPort hostPort = new HostAndPort("43.136.67.62", 6379);
            jedis = new Jedis(hostPort, config);
        }
    }


    @Test
    public void testLpush(){
        long l = jedis.lpush("list1", "1", "2", "3", "4", "5", "6", "7");
        l = jedis.lpush("list1","x","y","z");
        log.info(l+"");
    }

    @Test
    public void testLen(){
        long list1 = jedis.llen("list1");
        log.info(list1+"");
    }

    @Test
    public void testLpop(){
        String list1 = jedis.lpop("list1");
        log.info(list1);
        long llen = jedis.llen("list1");
        log.info(llen+"");
    }


    @Test
    public void testLpushx(){
        // 键存在的时候，才能push进去
        long list2 = jedis.lpushx("list2", "1", "2", "3", "4", "5", "6", "7");
        log.info(list2+"");

        // 追加到了list1中
        list2 = jedis.lpushx("list1", "a", "b", "c", "d", "e", "f", "g");
        log.info(list2+"");
    }


    @Test
    public void testLrange() {
        // 获取列表中一段区间的数据
        List<String> strings = jedis.lrange("list1", 0, -1);
        strings.stream().forEach(log::info);
    }

    @Test
    public void testLindexAndLset() {
        String s = jedis.lset("list1", 1L, "1111");
        log.info(s);
    }

    @Test
    public void testLindex() {
        log.info(jedis.lindex("list1",1) + "");
    }

    @Test
    public void testInsert(){
        // 在某个元素的后面插入数据
        long linsert = jedis.linsert("list1", ListPosition.AFTER, "a", "ccc");
        log.info(linsert+"");
    }

    @Test
    public void testLrem(){
        jedis.lrem("list1",0,"2");
    }


    @After
    public void end() {
        jedis.close();
    }

}
